1
'*************************** Module Header ******************************'
2 ' Module Name: XMLSerialization.vb
3 ' Project: VBWebBrowserAutomation
4 ' Copyright (c) Microsoft Corporation.
6 ' This class is used to serialize an object to an XML file or deserialize an XML
9 ' This source is subject to the Microsoft Public License.
10 ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
11 ' All other rights reserved.
13 ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
14 ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
15 ' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
16 '*************************************************************************'
19 Imports System
.Xml
.Serialization
21 Public Class
XMLSerialization(Of T
)
24 ''' Serialize an object to an XML file.
26 Public Shared
Function SerializeFromObjectToXML(ByVal obj
As T
,
27 ByVal filepath
As String) As Boolean
28 If obj Is
Nothing Then
29 Throw
New ArgumentException("The object to serialize could not be null!")
32 Dim successed
As Boolean = False
33 Dim objType
As Type = obj
.GetType()
34 Using fs
As New FileStream(filepath
, FileMode
.Create
, FileAccess
.ReadWrite
)
35 Dim xs
As New XmlSerializer(objType
)
44 ''' Deserialize an XML file to an object.
46 Public Shared
Function DeserializeFromXMLToObject(ByVal filepath
As String) As T
47 If Not File
.Exists(filepath
) Then
48 Throw
New ArgumentException("The file does not exist!")
52 Using fs
As New FileStream(filepath
, FileMode
.Open
)
53 Dim xs
As New XmlSerializer(GetType(T
))
54 obj
= CType(xs
.Deserialize(fs
), T
)